//
//  UIDevice+Extension.swift
//  Example
//
//  Created by Slience on 2021/1/13.
//

import UIKit

public extension UIDevice {
    public class var isPortrait: Bool {
        if isPad {
            return true
        }
        if  statusBarOrientation == .landscapeLeft ||
                statusBarOrientation == .landscapeRight {
            return false
        }
        return true
    }
    public class var statusBarOrientation: UIInterfaceOrientation {
        UIApplication.shared.statusBarOrientation
    }
    public class var navigationBarHeight: CGFloat {
        if isPad {
            if #available(iOS 12, *) {
                return statusBarHeight + 50
            }
        }
        return statusBarHeight + 44
    }
    class var generalStatusBarHeight: CGFloat {
        isAllIPhoneX ? 44 : 20
    }
    public class var statusBarHeight: CGFloat {
        let statusBarHeight: CGFloat
        let window = UIApplication.shared.windows.first
        if #available(iOS 13.0, *),
           let height = window?.windowScene?.statusBarManager?.statusBarFrame.size.height {
            statusBarHeight = height
        } else {
            statusBarHeight = UIApplication.shared.statusBarFrame.size.height
        }
        return statusBarHeight
    }
    public class var topMargin: CGFloat {
        if isAllIPhoneX {
            return statusBarHeight
        }
        return safeAreaInsets.top
    }
    public class var leftMargin: CGFloat {
        safeAreaInsets.left
    }
    public class var rightMargin: CGFloat {
        safeAreaInsets.right
    }
    public class var bottomMargin: CGFloat {
        safeAreaInsets.bottom
    }
    public class var isPad: Bool {
        current.userInterfaceIdiom == .pad
    }
    public class var isAllIPhoneX: Bool {
        let safeArea = safeAreaInsets
        let margin: CGFloat
        if isPortrait {
            margin = safeArea.bottom
        }else {
            margin = safeArea.left
        }
        return margin != 0
    }
    
    public class var safeAreaInsets: UIEdgeInsets {
        if #available(iOS 11.0, *) {
            return UIApplication._keyWindow?.safeAreaInsets ?? .zero
        }
        return .zero
    }
    
    public class var screenSize: CGSize {
        if #available(iOS 14.0, *), ProcessInfo.processInfo.isiOSAppOnMac {
            return UIApplication._keyWindow?.frame.size ?? UIScreen.main.bounds.size
        }
        return UIScreen.main.bounds.size
    }
}

public extension UIApplication {
    public class var _keyWindow: UIWindow? {
        var window: UIWindow?
        if #available(iOS 13.0, *) {
            window = shared.windows.filter({ $0.isKeyWindow }).last
        } else {
            window = shared.delegate?.window ?? shared.keyWindow
        }
        return window
    }
}


public extension UIDevice {
    
    
    class func copyImage(image:UIImage) {
        UIPasteboard.general.image = image
    }
    
}

public extension UIDevice {
    /// 开启屏幕常亮
    static func keepScreenAwake() {
        UIApplication.shared.isIdleTimerDisabled = true
    }
    
    /// 恢复默认屏幕休眠行为
    static func allowScreenSleep() {
        UIApplication.shared.isIdleTimerDisabled = false
    }
    
    /// 0最小,1 最亮
    static func brightness(brightness:CGFloat) {
        UIScreen.main.brightness = brightness
    }
    
}
public extension UIDevice {
    static var IPad: Bool {
         // iOS 14+ 可以直接使用 model 属性
         if #available(iOS 14.0, *) {
             return current.model == "iPad"
         }
         // iOS 13 及以下回退到 userInterfaceIdiom
         else {
             return current.userInterfaceIdiom == .pad
         }
     }
}
